<template>
  <div class="app-container">
    <div style="width: 100%; margin-bottom: 10px">
      <el-button type="primary" size="mini" @click="addCommand">添加</el-button>
<!--      <el-button-->
<!--        type="warning"-->
<!--        plain-->
<!--        icon="el-icon-download"-->
<!--        size="mini"-->
<!--        @click="handleExport"-->
<!--      >导出</el-button>-->
    </div>
    <el-table border :data="detailList" style="width: 100%">
      <el-table-column label="编号" width="50" type="index" align="center" fixed="left"
                       :index="index=>index+1"/>

      <el-table-column prop="big" label="考核大项" align="left" :width="200">
        <template #default="scope">
          <p style="white-space: pre-wrap;" v-show="!scope.row.editFlag">{{scope.row.big}}</p>
          <el-input type="textarea" v-show="scope.row.editFlag"
                    v-model="scope.row.big">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="small" label="考核小项" align="left" :width="300">
        <template #default="scope">
          <p style="white-space: pre-wrap;" v-show="!scope.row.editFlag">{{scope.row.small}}</p>
          <el-input type="textarea" v-show="scope.row.editFlag"
                    v-model="scope.row.small">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column prop="standard" label="考核标准" align="left" :width="300">
        <template #default="scope">
          <p style="white-space: pre-wrap;" v-show="!scope.row.editFlag">{{scope.row.standard}}</p>
          <el-input type="textarea" v-show="scope.row.editFlag"
                    v-model="scope.row.standard">
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" :width="100" align="center"/>
      <el-table-column label="更新人" prop="updateBy" :width="100" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" :width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" :width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="140">
        <template #default="scope">
          <div style="">
            <el-button size="small"
                       type="text"
                       icon="CircleCheck"
                       v-show="scope.row.editFlag"
                       @click="submit(scope.row, $index)">保存
            </el-button>
            <el-button size="small"
                       type="text"
                       icon="CircleClose"
                       v-show="scope.row.editFlag"
                       @click="cancel(scope.row, $index)">取消
            </el-button>
            <el-button size="small"
                       icon="Edit"
                       type="text"
                       v-show="!scope.row.editFlag"
                       @click="edit(scope.row, $indx)"> 编辑
            </el-button>
            <el-button size="small"
                       icon="Delete"
                       type="text"
                       v-show="!scope.row.editFlag"
                       @click="del(scope.row, $index)">删除
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body @close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="考核大项" prop="big">
          <el-input type="textarea" v-model="form.big" placeholder="请输入考核大项" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="考核小项" prop="small">
          <el-input type="textarea" v-model="form.small" placeholder="请输入考核小项" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="考核标准" prop="standard">
          <el-input type="textarea" v-model="form.standard" placeholder="请输入考核标准" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="close()">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { list, listAll, get, add, update, del } from "@/api/social/examine/planDetail";
import {blurRule} from "@/utils/formRules";

export default {
  name: "PlanDetail",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      id: undefined,
      // 遮罩层
      loading: true,
      // 菜品明细表格数据
      detailList: [],
      garbageTypeOptions: [],
      form: {},
      rules:{
        big:blurRule('请输入考核大项'),
        small:blurRule('请输入考核小项'),
        standard:blurRule('请输入考核标准'),
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false
    };
  },
  created() {
    this.id = this.$route.params && this.$route.params.id;
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAll({
        planId: this.id,
      }).then(response => {
          this.detailList = response.rows;
          this.loading = false;
        }
      );
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if(valid){
          add(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      })
    },
    addCommand: function() {
      this.form = {
        planId: this.id,
      };
      this.open = true;
      this.title = "添加";
    },
    submitApprove: function() {

    },
    submit: function(row, index) {
      if (!row.big) {
        this.$modal.msgError("考核大项不能为空");
        return;
      }
      if (!row.small) {
        this.$modal.msgError("考核小项不能为空");
        return;
      }
      if (!row.standard) {
        this.$modal.msgError("考核标准不能为空");
        return;
      }
      let updateData = {
        id: row.id,
        big: row.big,
        small: row.small,
        standard: row.standard
      }
      update(updateData).then(res => {
        this.$modal.msgSuccess("更新成功");
        row.editFlag = false;
        this.getList();
      })
    },
    cancel: function(row, index) {
      row.editFlag = false;
      this.getList();
    },
    edit: function(row, index) {
      row.editFlag = true;
      this.$set(this.detailList, index, row);
    },
    del: function(row, index) {
      this.$confirm('请确认是否删除该考核明细').then(res=>{
        del(row.id).then(response => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        });
      })
    },
    close(){
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
  }
};
</script>
